package br.com.fiap.agenteDialogo.BLL;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import org.apache.commons.collections15.Transformer;
import br.com.fiap.agenteDialogo.Beans.Edge;
import br.com.fiap.agenteDialogo.Beans.Vertex;
import edu.uci.ics.jung.algorithms.layout.AbstractLayout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.Hypergraph;
import edu.uci.ics.jung.io.GraphMLWriter;

public class AgenteDialogoXml {

	public static void importarXml()
	{
		
	}
	
	public static boolean exportarXml(Graph<Vertex, Edge> graph, final AbstractLayout<Vertex, Edge> layout, String caminhoArquivo)
	{	
		boolean arquivoExportado = false;
		
		try {
		
			GraphMLWriter<Vertex, Edge> graphWriter = new GraphMLWriter<Vertex, Edge>();
			
			graphWriter.setVertexIDs(new Transformer<Vertex, String>() {
				public String transform(Vertex v) {   
					return "V" + Integer.toString(v.getID());
				}
		    });
			
			graphWriter.setVertexDescriptions(new Transformer<Vertex, String>() {
				public String transform(Vertex v) {   
					return  v.getValue();
				}
		    });
			
			
			graphWriter.setEdgeIDs(new Transformer<Edge, String>() {
				public String transform(Edge e) {   
					return "E" + Integer.toString(e.getID());
				}
		    });
			
			
			graphWriter.setEdgeDescriptions(new Transformer<Edge, String>() {
				public String transform(Edge e) {   
					return e.getValue();
				}
		    });
		
			PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(caminhoArquivo)));
		
			Hypergraph<Vertex, Edge> g = graph;
			
			graphWriter.save(g, out);
			
			out.close();
			
			arquivoExportado = true;
			
		} catch (Exception e) {
			e.printStackTrace();
			arquivoExportado = false;
		}
		
		return arquivoExportado;
	}
}
